namespace :bld do
  desc "Get data"
  task :get_acr_data => :environment do
    date_now =  Time.now
    ac_types = ENV['codes'] ? AcType.find_all_by_page_code(ENV['codes'].split(',')) : AcType.find_all_by_code_type(0)
    pages = Scan.search ac_types
    puts "#{Time.now - date_now} second complete"
    date_now =  Time.now
    puts "processing..."
    begin
      ActiveRecord::Base.transaction do
        scan = Scan.create! :description => "#{DateTime.now} 创建的记录"
        pages.each_with_index do |page_ary, i|
          Scan.work_info_fixed page_ary[1], page_ary[0]#, true
          scan.add_results page_ary[1]
        end
      end
    rescue
      p $!,$@
    end
    puts "#{Time.now - date_now} second complete"
  end

  desc "Get single info"
  task :get_work_info => :environment do
    works = Work.find :all, :conditions => "#{'part_count is null and ' unless ENV['all']} status = 1", :order => "wid"
      works.each do |work|
        print "|==#{work.wid.to_s}:"
        work.get_info
        print %< #{work.name} #{work.part_count}p\n>
      end
  end

  desc "ranking work info fixed"
  task :ranking_work_info_fixed => :environment do
    puts "=====begin to ranking fixed====="
    date_now =  Time.now
    id1 = ENV['id1'] || (YAML.load(open(File.join "#{RAILS_ROOT}/config/last.yml")) rescue 0)
    id2 = ENV['id2'] || Scan.last.id
    results = WeeklyResult.stat(id1, id2, [-1])
    #Point序
    results.each_with_index do |r, i|
      print "."
      r.work.get_info false
      break if i == 200#只修复粗略计算的前多少名
    end
    print "\n"
    puts "#{Time.now - date_now} second complete"
  end

  desc "Get author only"
  task :get_authors => :environment do
    works = Work.find :all, :conditions => "status = 1 and author_id is null", :order => "wid"
      works.each do |work|
        print "|==wid:" + work.wid.to_s
        work.get_info false
        print "  " + work.author_id.to_s + "\n"
      end
  end

  desc "Get danmu data (not used at time)"
  task :get_d_data => :environment do
    require 'rexml/document'
    require 'net/http'
    require 'zlib'
    scan = Scan.find_by_id ENV['scan_id']
    scan = Scan.last unless scan
    flash_vars = FlashVar.find :all, :include => :work, :conditions => ["works.cdate > ?", Date.today.beginning_of_month]
    h_flash_vars = DResult.find :all, :include => :flash_var, :conditions =>  ["d_results.scan_id = ?", scan.id]
    h_flash_var_ids = h_flash_vars.collect{|hfv| hfv.flash_var.id}
    Net::HTTP.version_1_2
    Net::HTTP.start("d.#{BILI_PATH}") do |http|
      flash_vars.each do |fv|
        unless h_flash_var_ids.include?(fv.id)
          response = http.get( "/" + fv.vid.rstrip )
          if response['Content-Encoding'] == 'deflate'
            doc = REXML::Document.new(Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(response.body))
          else
            doc = REXML::Document.new(response.body)
          end
          ori_data = doc.elements.to_a( "//d")
          DResult.create :scan_id => scan.id, :flash_var_id => fv.id, :d_count => ori_data.size
          p "#{fv.vid} : #{ori_data.size}"
        end
      end
    end
  end

  desc "Get danmu data from search"
  task :fix_d_data_from_search => :environment do
    scan_id = ENV['scan_id'] ? ENV['scan_id'].to_i : Scan.last.id
    results = Result.find_all_by_scan_id scan_id, :include => {:work => [:ac_type, :author]}, :conditions => "danmu >= 1500 or danmu = 0"
    results.each do |result|
      if result.work.part_count == 1 && (result.danmu == 1500 || result.danmu == 2000 || result.danmu == 3000) || (result.work.part_count && result.work.part_count > 1) && result.danmu > 1500
        result.fix_danmu
      elsif result.danmu.zero? && result.clicks > 200
        print "====#{result.work.wid} #{result.danmu} => "
        result.update_attribute :danmu, result.work.flash_vars.inject(0){|re, item| re += item.get_danmu_count}
        p result.danmu
      end
    end
  end
end
